<template>
  <pro-layout
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :i18nRender="i18nRender"
    v-bind="settings"
  >
    <!-- Ads begin
      广告代码 真实项目中请移除
      production remove this Ads
    -->
    <!--<ads v-if="isProPreviewSite && !collapsed"/>-->
    <!-- Ads end -->

    <!-- 1.0.0+ 版本 pro-layout 提供 API，
          我们推荐使用这种方式进行 LOGO 和 title 自定义
    -->
    <template v-slot:menuHeaderRender>
      <!-- <div class="logoImage"></div> -->
      <div style=" color: #fff; font-size: 24px;">品新工业云SAAS</div>
    </template>

    <!--左边菜单-->
    <template v-slot:menuRender>
    <div class='menuListWrap'>
      <div class='menuList'>
        <div class='menuLi'
          :class='_.some($route.matched, { name: n.name }) && "menuLiActive"'
          v-for='(n,i) in menus' :key='i' @click='linkOut(n,i)'>
          <a-icon :type='n.meta.icon' />
          &nbsp;
          <span v-if='!collapsed'>{{ n.meta && n.meta.title }}</span>
        </div>
      </div>
      <div class='menuList2' v-if='!collapsed'>
        <div class='menuLi2 menuLi2Active'>{{matchRight.meta && matchRight.meta.title}}</div>
        <div class='menuLi2'
          :class='_.some($route.matched, { name: n.name }) && "menuLi2Active"'
          v-for='(n,i) in matchRight.children' :key='i' @click='linkOut2(n,i)'>
          {{n.meta && n.meta.title}}
        </div>
      </div>
    </div>
    </template>

    <!-- 1.0.0+ 版本 pro-layout 提供 API,
          增加 Header 左侧内容区自定义
    -->
    <template v-slot:headerContentRender>
    <div>
      <!-- <a-tooltip title='刷新页面'>
        <a-icon type='reload' style='font-size: 18px;cursor: pointer;' @click='refresh()' />
      </a-tooltip> -->
      <div class='topMenuList'>
        <div class='topMenuLi'
          :class='(topMenuListActiveKey === n.key) && "topMenuLiActive"'
          v-for='(n,i) in topMenuList' :key='i' @click='topMenuClick(n,i)'
        >{{ n.label }}</div>
      </div>
    </div>
    </template>

    <setting-drawer :settings="settings" @change="handleSettingChange">
      <div style="margin: 12px 0;">
        This is SettingDrawer custom footer content.
      </div>
    </setting-drawer>
    <template v-slot:rightContentRender>
      <right-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile" :theme="settings.theme" />
    </template>
    <!-- custom footer / 自定义Footer -->
    <!-- <template v-slot:footerRender> -->
      <!-- <global-footer /> -->
    <!-- </template> -->

    <router-view />
  </pro-layout>
</template>

<script>
import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'
import { asyncRouterMap2 } from '@/config/router.config'

import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'
import Ads from '@/components/Other/CarbonAds'
import LogoSvg from '../assets/logo.svg?inline'
import router from '@/router'
import store from '@/store'

export default {
  inject: ['reload'], // 引入方法
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    RightContent,
    GlobalFooter,
    LogoSvg,
    Ads
  },
  data() {
    return {
      topMenuList: [
        { label: '运营管理中心', key: 'a' },
        { label: '解决方案', key: 'b' },
        { label: '云市场', key: 'c' },
        { label: '技术支持', key: 'd' },
        { label: '合作与生态', key: 'e' },
        { label: 'ERP', key: 'f' },
        { label: 'MES', key: 'g' },
        { label: 'WMS', key: 'h' }
      ],
      topMenuListActiveKey: 'a',
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: defaultSettings.title,
      settings: {
        // 布局类型
        layout: defaultSettings.layout, // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,

        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters
    }),
    matchRight(){
      let matchRight = {}
      _.each(this.$route.matched, (n, i) => {
        _.each(this.menus, (nn, ii) => {
          if (n.name === nn.name) {
            matchRight = nn
          }
        })
      })
     return matchRight
    },
  },
  watch: {
    // '$route' (to, from) {
    //
    //   console.log(`this.$route.matched`, this.$route.matched)
    //   console.log(`this.menus`, this.menus)
    //
    //   let matchList = []
    //   _.each(this.$route.matched, (n, i) => {
    //     _.each(this.menus, (nn, ii) => {
    //       if (n.name === nn.name) {
    //         matchList = nn.children
    //       }
    //     })
    //   })
    //   this.menus2 = matchList
    // }
  },
  created() {
    const routes = this.mainMenu.find(item => item.path === `/${this.topMenuListActiveKey}`)
    this.menus = (routes && routes.children) || []
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted() {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    topMenuClick(n, i){
      this.topMenuListActiveKey = n.key
      // store.dispatch('GenerateRoutes', { roles: { permissionList: [] }, router:asyncRouterMap2 }).then(() => {
      //   router.addRoutes(store.getters.addRouters)
      // })

      const routes = this.mainMenu.find(item => item.path === `/${this.topMenuListActiveKey}`)
      this.menus = (routes && routes.children) || []
      this.$router.push(`/${this.topMenuListActiveKey}`)
    },
    linkOut(n, i){
      console.log(`n`, n)
      this.$router.push(n.pathAbs)
    },
    linkOut2(n, i){
      console.log(`n`, n)
      this.$router.push(n.pathAbs)
    },
    i18nRender,
    handleMediaQuery(val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
        // this.settings.fixSiderbar = false
      }
    },
    handleCollapse(val) {
      this.collapsed = val
    },
    handleSettingChange({ type, value }) {
      console.log('type', type, value)
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    },
    refresh() {
      this.reload() // 调用方法
    }
  }
}
</script>

<style lang="less">
@import './BasicLayout.less';
// .ant-pro-basicLayout-content{
//   margin: 0;
// }
// .ant-layout-footer{
//   padding: 0;
// }
.logoImage {
  width: 80%;
  height: 55px;
  background: url('../assets/logo2.png');
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 80% auto;
  margin-top: 10px;
}

.menuListWrap {
  height: calc(100% - 64px);
  overflow: auto;
  display: flex;
  justify-content: center;
  align-items: stretch;

  .menuList {
    width: 50%;
    flex:1;

    .menuLi {
      color: white;
      padding: 20px;
cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .menuLiActive {
      background-color: white;
      color: #1A86FE;
    }
  }

  .menuList2 {
    width: 50%;
    flex: 1;
    background-color: white;

    .menuLi2 {
      color: #CCCCCC;
      padding: 20px;
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .menuLi2Active {
      background-color: #444444;
      color: white;
    }
  }
}


//.ant-menu-dark.ant-menu-inline, .ant-menu-dark.ant-menu-vertical, .ant-menu-dark.ant-menu-vertical-left, .ant-menu-dark.ant-menu-vertical-right{
//  width: 50%;
//}
//
//.ant-menu-dark .ant-menu-inline.ant-menu-sub {
//  position: absolute;
//  top: 64px;
//  width: 50%;
//  right: 0;
//}

.ant-pro-global-header {
  background-color: #2175FF;
}
.ant-pro-sider-menu-logo{
  background-color: #2175FF;
}

.topMenuList {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;

  .topMenuLi {
    padding: 0 10px;
    color: white;
    min-width: 100px;
    text-align: center;
  }

  .topMenuLiActive {
    background-color: white;
    color: #2175FF;
  }
}

</style>
